//
//  UserDataCenter.m
//  Safflower
//
//  Created by 袁亮 on 2016/11/28.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "UserDataCenter.h"

@implementation UserDataCenter

-(void)userLoginWithPhone:(NSString *)phone password:(NSString *)password complation:(void (^)(id))complation
{
    NSDictionary *param = @{@"phone":phone,@"password":password};
    
    [HttpRequest POSTRequest:UserLogin WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            NSDictionary *item = (NSDictionary *)[responseOjbect objectForKey:@"data"];
            UserModel *user = [UserModel yy_modelWithJSON:item];
            [UserManager saveUser:user];
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

-(void)sendVerifyWithPhone:(NSString *)phone complation:(void (^)(id))complation
{
    NSDictionary *param = @{@"phone":phone};
    
    [HttpRequest POSTRequest:SendVerify WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

-(void)verifyCodeWithPhone:(NSString *)phone code:(NSString *)code complation:(void (^)(id))complation
{
    NSDictionary *param = @{@"verifyNum":code,@"phone":phone};
    
    [HttpRequest POSTRequest:VerifyCode WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

-(void)regsitAccountWithUser:(UserModel *)user password:(NSString *)password complation:(void (^)(id))complation
{
    NSDictionary *param = @{
                            @"username":user.username,
                            @"sex":user.sex,
                            @"sign":user.sign,
                            @"phone":user.phone,
                            @"password":password,
                            };
    
    [HttpRequest POSTRequest:RegsitAccount WithParams:param WithSuccess:^(id responseOjbect) {
        
        if ([[responseOjbect objectForKey:@"code"] integerValue] == 1) {
            complation(nil);
        }else{
            complation([responseOjbect objectForKey:@"message"]);
        }
        
    } WithFail:^(NSError *error) {
        complation(@"服务器异常");
    }];
}

@end
